import 'dart:math';

import 'package:flutter/material.dart';
import 'package:tiktokadult/assets_tool/app_colors.dart';
import 'package:tiktokadult/hj_page/home_business/home_cell_style/video_simple_cell_widget.dart';
import 'package:tiktokadult/tools_base/loading/l_error_widget.dart';
import 'package:tiktokadult/tools_base/loading/loading_center_widget.dart';

import '../../../hj_model/home/home_tabs_entity.dart';
import '../../../hj_model/home/module_detail_model.dart';
import '../../../hj_model/video_model.dart';
import '../../../hj_utils/sliver_delegate.dart';
import '../home_sub_module/home_tab_section_logic.dart';
import '../widget/home_sort_menu_view.dart';

class HomeGuessLikeSliver extends StatelessWidget {
  final AllSection model;
  final bool? isDarkStyle;
  final int? sortIndex;
  final ModuleData? tabModel;
  final HomeTabSectionLogic? logic;

  HomeGuessLikeSliver(
    this.model, {
    this.isDarkStyle,
    this.sortIndex,
    this.tabModel,
    this.logic,
  });

  @override
  Widget build(BuildContext context) {
    return SliverMainAxisGroup(
      slivers: [
        SliverToBoxAdapter(
          child: Container(
            padding: EdgeInsets.only(left: 12),
            child: Text(
              "猜你喜欢",
              style: TextStyle(
                fontSize: 18,
                color: Color(0xE5FFFFFF),
                fontWeight: FontWeight.w600,
              ),
            ),
          ),
        ),
        SliverPersistentHeader(
          pinned: true,
          floating: true,
          delegate: MySliverDelegate(
            maxHeight: 42,
            minHeight: 42,
            childBuildHandler: (_, offset, overlaps, child) {
              return Container(
                color: (offset >= 0) ? AppColors.primaryColor : Colors.transparent,
                child: child,
              );
            },
            child: Container(
              alignment: Alignment.centerLeft,
              padding: EdgeInsets.only(left: 12),
              child: HomeSortMenuView(
                ["最多收藏", "最新上架", "最多观看"],
                gapChar: "|",
                selectIndex: sortIndex ?? 0,
                callback: (index) {
                  if (index != sortIndex) {
                    logic?.sortGuessLikeExchange(index);
                  }
                },
              ),
            ),
          ),
        ),
        if (model.allVideoInfo == null)
          SliverToBoxAdapter(
            child: SizedBox(height: 300, child: LoadingCenterWidget()),
          )
        else if (model.allVideoInfo!.isEmpty)
          SliverToBoxAdapter(
            child: SizedBox(height: 300, child: CErrorWidget()),
          )
        else
          SliverPadding(
            padding: EdgeInsets.only(left: 12, right: 12),
            sliver: SliverGrid(
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 2,
                mainAxisSpacing: 12,
                crossAxisSpacing: 8,
                childAspectRatio: 168 / 154,
              ),
              delegate: SliverChildBuilderDelegate(
                (context, index) {
                  VideoModel data = model.allVideoInfo![index];
                  return VideoSimpleCellWidget(videoModel: data);
                },
                childCount: max(0, model.allVideoInfo?.length ?? 0),
              ),
            ),
          ),
      ],
    );
  }
}
